<template>
  <div class="container">
    <div class="headTitle">
      <div class="icons" @click="backTo">
        <span class="iconfont">&#xe799;</span>
      </div>
      <div class="titles">物流客服</div>
      <div class="introduction"></div>
    </div>
    <div>
      <div class="showTalking">
        <div></div>
        <div></div>
      </div>
    </div>
    <div class="footer">
      <div class="textArea">
        <input type="text" v-model="contentText"></input>
      </div>
      <div class="submits">
        <button @click="sendText">发送</button>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "consult",
    methods: {
      backTo(){
        this.$router.push('/userMine');
      },
      //发送聊天信息
      sendText() {
        let that = this;
        this.list = [...this.list, {type: "mine", content: this.contentText}];//通过type字段进行区分是自己（mine）发的还是系统（robot）返回的
        this.backText(function () {
          that.contentText = "";//加回调在得到返回数据的时候清除输入框的内容
        });
      },
      backText(callback) {
        let that = this;
        if (window.WebSocket) {
          let ws = new WebSocket("ws://192.168.11.169:8001");
          ws.onopen = function (e) {
            console.log("链接服务器成功");
            console.log("that.contentText is", that.contentText);
            ws.send(that.contentText);
            callback();
          };
          ws.onclose = function (e) {
            console.log("服务器关闭")
          };
          ws.onerror = function () {
            console.log("服务器出错")
          };
          ws.onmessage = function (e) {
            that.list = [...that.list, {type: "robot", content: e.data}]
          }
        }
      }
    },
    watch: {
      //监听list,当有修改的时候进行div的屏幕滚动，确保能看到最新的聊天
      list: function () {
        let that = this;
        setTimeout(() => {
          that.$refs.box.scrollTop = that.$refs.box.scrollHeight;
        }, 0);
        //加setTimeout的原因：由于vue采用虚拟dom，我每次生成新的消息时获取到的div的scrollHeight的值是生成新消息之前的值，所以造成每次都是最新的那条消息被隐藏掉了
      }
    },
    data(){
      return{
        list: [],//聊天记录的数组
        contentText: "",//input输入的值
      }
    },
    mounted() {
      if (window.WebSocket) {
        let ws = new WebSocket("ws://192.168.11.169:8001");
        ws.onopen = function (e) {
          console.log("链接服务器成功");
          console.log("that.contentText is", that.contentText);
          ws.send(that.contentText);
          callback();
        };
        ws.onclose = function (e) {
          console.log("服务器关闭")
        };
        ws.onerror = function () {
          console.log("服务器出错")
        };
        ws.onmessage = function (e) {
          that.list = [...that.list, {type: "robot", content: e.data}]
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  /*引入图标*/
  @import url("../../../assets/userIconFont/iconfont.css");
  .container{
    .headTitle{
      background-color: white;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 60px 20px 20px 20px;
      margin-bottom: 5px;
      .icons{
        span{
          font-size: 40px;
          color: #8590a6;
        }
      }
      .titles{
        font-size: 35px;
      }
      .introduction{
        font-size: 30px;
      }
    }
    .showTalking{
      text-align: center;
    }
    .footer{
      display: flex;
      justify-content: space-around;
      position: absolute;
      align-items: center;
      bottom: 0;
      background-color: #00c6ff;
      padding: 10px;
      width: 100%;
      .textArea{
        input{
          flex-grow: 1;
          height: 100px;
          width: 600px;
          border-radius: 50px;
          border:none;
          outline: 0;
        }
      }
      .submits{
        margin-right: 20px;
        button{
          padding: 10px 20px;
          border-radius: 20px;
          background-color: aqua;
          color: white;
          outline: 0;
          border: none;
        }
      }
    }
  }
</style>